package com.bite.springblog.common.utils;

import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class SecurityUtil {
    //加密
    public static String encrypt(String password){
        String salt = UUID.randomUUID().toString().replace("-","");
        String s = DigestUtils.md5DigestAsHex((salt + password).getBytes(StandardCharsets.UTF_8));
        return salt+s;
    }

    //验证密码
    public static boolean verify(String password,String encryptPassword){
        if(!StringUtils.hasLength(password)){
            return false;
        }
        if(encryptPassword == null || encryptPassword.length()!=64){
            return false;
        }
        String salt = encryptPassword.substring(0,32);
        String s = DigestUtils.md5DigestAsHex((salt + password).getBytes(StandardCharsets.UTF_8));
        return encryptPassword.equals(salt+s);
    }

//    public static void main(String[] args) {
//        String password = "123456";
//        String salt = UUID.randomUUID().toString().replace("-","");
//        System.out.println(salt);
//        String encryptPassword = salt + DigestUtils.md5DigestAsHex((salt + password).getBytes(StandardCharsets.UTF_8));
//        System.out.println(encryptPassword);
//        System.out.println(verify("12345",encryptPassword));
//    }
}




